package com.campusmarket.controller;

import com.campusmarket.common.base.BaseController;
import com.campusmarket.service.CommodityService;
import com.campusmarket.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class FrontController extends BaseController {

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/index.html",produces = "text/html;charset=utf-8")
    public String index(){
        //如果已经登陆,去到相应主页
        //return "interface/login";
        //否则去到登陆界面
        return "interface/login";
    }

    @RequestMapping(value = "/register.html",produces = "text/html;charset=utf-8")
    public String register(){
        return "interface/register";
    }

    @RequestMapping(value = "/login.html",produces = "text/html;charset=utf-8")
    public String login() {return "interface/login";}

    @RequestMapping(value="/user.html", produces="text/html;charset=utf-8")
    public String user(){return "interface/user";}

    @RequestMapping(value="/user_edit.html", produces="text/html;charset=utf-8")
    public String user_edit(){return "interface/user_edit";}

    @RequestMapping(value="/admin_edit.html", produces="text/html;charset=utf-8")
    public String admin_edit(){return "interface/admin_edit";}

    @RequestMapping(value="/user_check.html", produces="text/html;charset=utf-8")
    public String user_check(){return "interface/user_check";}

    @RequestMapping(value="/administrator.html", produces="text/html;charset=utf-8")
    public String administrator(){return "interface/administrator";}

    @RequestMapping(value="/ad_com_operation.html", produces="text/html;charset=utf-8")
    public String ad_com_operation(){return "interface/ad_com_operation";}

    @RequestMapping(value="/admin_operation_com.html", produces="text/html;charset=utf-8")
    public String admin_operation_com(){return "interface/admin_operation_com";}

    @RequestMapping(value="/ad_user_operation.html", produces="text/html;charset=utf-8")
    public String ad_user_operation(){return "interface/ad_user_operation";}

    @RequestMapping(value="/admin_operation_user.html", produces="text/html;charset=utf-8")
    public String admin_operation_user(){return "interface/admin_operation_user";}

    @RequestMapping(value="/add_commodity.html", produces="text/html;charset=utf-8")
    public String add_commodity(@RequestParam(value="id",required = false) Long id, Model model){
        if(id!=null){
            try {
                model.addAttribute("commodity",commodityService.queryById(id));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "interface/add_commodity";
    }

    @RequestMapping(value="/admin_add_com.html", produces="text/html;charset=utf-8")
    public String admin_add_com(@RequestParam(value="id",required = false) Long id, Model model){
        if(id!=null){
            try {
                model.addAttribute("commodity",commodityService.queryById(id));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "interface/admin_add_com";
    }

    @RequestMapping(value="/add_user.html", produces="text/html;charset=utf-8")
    public String add_user(@RequestParam(value="id",required = false) Long id, Model model){
        if(id!=null){
            try {
                model.addAttribute("user",userService.queryById(id));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "interface/add_user";
    }

    @RequestMapping(value="/admin_add_user.html", produces="text/html;charset=utf-8")
    public String admin_add_user(@RequestParam(value="id",required = false) Long id, Model model){
        if(id!=null){
            try {
                model.addAttribute("user",userService.queryById(id));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "interface/admin_add_user";
    }



    @RequestMapping(value="/coin_order.html", produces="text/html;charset=utf-8")
    public String coin_order(){return "interface/coin_order";}
}
